package com.xyh.transaction.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xyh.transaction.common.Current;
import com.xyh.transaction.entity.Button;
import com.xyh.transaction.entity.Menu;
import com.xyh.transaction.entity.Role;
import com.xyh.transaction.entity.dto.button.ButtonEditOrAddDto;
import com.xyh.transaction.entity.dto.button.ButtonSearchDto;
import com.xyh.transaction.entity.dto.user.ArrDto;
import com.xyh.transaction.exception.DeleteException;
import com.xyh.transaction.exception.UpdateException;
import com.xyh.transaction.mapper.ButtonMapper;
import com.xyh.transaction.mapper.MenuMapper;
import com.xyh.transaction.mapper.RoleMapper;
import com.xyh.transaction.service.ButtonService;
import com.xyh.transaction.utils.MappingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class ButtonServiceImpl implements ButtonService {

    @Autowired
    private ButtonMapper buttonMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public Map<String, List<String>> getUserAuthButtonList() {
        HashMap<String, List<String>> buttonAuthMap = new HashMap<>();

        Integer userId = Current.getCurrentUser().getUser().getId();

        Set<Role> userRoles = roleMapper.findRoleByUserId(userId);

        Set<Integer> uniqueMenuIds = new HashSet<>();
        userRoles.forEach(role -> roleMapper.findMenuIdListByRoleId(role.getId()).forEach(menuId -> {
            Set<Integer> recursiveMenuIds = menuMapper.findRecursiveById(menuId).stream()
                    .map(Menu::getId)
                    .collect(Collectors.toSet());
            uniqueMenuIds.addAll(recursiveMenuIds);
        }));

        uniqueMenuIds.stream()
                .flatMap(menuId -> buttonMapper.findButtonAuthByMenuId(menuId).stream())
                .forEach(buttonDto -> {
                    String name = buttonDto.getName();
                    String buttonAuth = buttonDto.getButtonAuth();
                    if (buttonAuth != null) {
                        buttonAuthMap.computeIfAbsent(name, k -> new ArrayList<>()).add(buttonAuth);
                    }
                });

        return buttonAuthMap;
    }

    @Override
    public PageInfo<Button> getButtonAuthList(ButtonSearchDto buttonSearchDto) {

        Button button = MappingUtil.mapObject(buttonSearchDto, Button.class);

        Integer pageSize = buttonSearchDto.getPageSize();
        Integer pageNum = buttonSearchDto.getPageNum();

        if (pageSize != null && pageNum != null){
            PageHelper.startPage(pageNum,pageSize);
        }

        List<Button> buttonList = buttonMapper.findAll(button);

        return new PageInfo<>(buttonList);
    }

    @Override
    public void addButton(ButtonEditOrAddDto buttonEditOrAddDto) {
        Button button = MappingUtil.mapObject(buttonEditOrAddDto, Button.class);

        buttonMapper.inset(button);
    }

    @Override
    public void editButton(ButtonEditOrAddDto buttonEditOrAddDto) {
        Button button = MappingUtil.mapObject(buttonEditOrAddDto, Button.class);

        Integer uRows = buttonMapper.updateById(button);

        if (uRows <= 0){
            throw new UpdateException("修改按钮权限信息失败，未更新任何数据");
        }
    }

    @Override
    public void deleteButton(ArrDto arrDto) {

        Integer[] ids = arrDto.getId();

        Integer dRows = buttonMapper.deleteByIds(ids);

        if (dRows <= 0){
            throw new DeleteException("删除按钮权限失败，未删除任何记录");
        }

    }
}
